#ifndef CONNECTION_H
#define CONNECTION_H

//**************************************************************
//
//                    CAM SCANNER - PROJECT
//
// This class give all network functions to the main class.
// It will create a connection to the server and send the
// images to it.
//
// version 1.0 - 25.06.2012 Adriano:
//    : first version
//
//
//
//
//
//
//
//**************************************************************


#include <QWidget>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
#include <QTcpSocket>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QTimer>

class connection : public QWidget
{
    Q_OBJECT
public:
    explicit connection(QWidget *parent = 0);

        // return the IP of the server
    QString getSavedIP(void);

        // send im to the server as bytestrem.
    void sendImageOverWiFi(QImage im);

        // check if server can be found on the saved IP.
        // This function is public, so if the connection was lost,
        // also other classes can proof the availability of the server.
    bool checkIP(void);

        // Last message from this class, only for debug purposes.
    QString getMessage(void);

private:
    QHostAddress    m_hostAddress;      // Right host address, connection will be made on this address
    QString         m_sFileIP;          // Once the server was connected, the IP is saved to a file so the next time this IP appears again
    QLabel*         m_labelWifi;        // label - showing a wireless icon blinking
    QLabel*         m_labelIP;          // label - showing text for LineEdit - "insert IP"
    QTimer*         m_timerBlink;       // timer - blink wifi icon on labelWifi
    bool            m_bBlinkState;      // blinking state for labelWifi (blink blue/red)
    int             m_iCheckedState;    // server connection state. 0=not connected, 1=ready to connect, 2=connected
    bool            m_bConnectionOK;    // server was connected successfully

    QLineEdit*      m_lineEdit;         // lineedit - insert IP address of the server
    QPushButton*    m_buttonSearchServer; // button - search over 255 ip addresses to find server
    QPushButton*    m_buttonExit;       // button   - exit application
    QPushButton*    m_buttonConnect;    // button   - try to connect to lineEdit-IP number
    QPlainTextEdit* m_plainTextEdit;    // textbox  - debug outputs

    QNetworkAccessManager* m_openWiFiConnection; // manager - access a webpage so a connection will be estabilished at begins
    
signals:
    void connected(void);               // signal emitted, when connection is estabilished
    
public slots:
    void blinkImage(void);              // slot for timer, so the labelWifi can be updated with the new image
    void searchServer(void);            // slot for button, search all address for a server
    void exitApp(void);                 // slot for button, close app
    void textEdited(void);              // slot for lineedit, check ip address if text was edited
    void tryConnect(void);              // slot for button, try to connect to the server (like textEdited slot)
    void networkAccessible(QNetworkAccessManager::NetworkAccessibility acc); // manager has a connection to the web, try to search the server
    
};

#endif // CONNECTION_H
